package com.example.day03homework.DAO;

import com.example.day03homework.Utils.JDBCUtils;
import com.example.day03homework.POJO.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*查询用户表*/
public class UserList {
    private Connection con;
    List<User> listUser = new ArrayList<User>();
    public List<User> getUser() throws SQLException {
        con = JDBCUtils.getConnection();
        String sql = "select * from user";
        PreparedStatement pst = con.prepareStatement(sql);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            Integer id = rs.getInt("id");
            String userName = rs.getString("userName");
            String passWord = rs.getString("passWord");
            String information = rs.getString("information");
            User user = new User(id, userName, passWord, information);
            listUser.add(user);
        }
        JDBCUtils.close(rs, pst, con);
        return listUser;
    }
}
